package leetcode.N101_N200;

import leetcode.comm.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/15 8:52 下午
 */
public class N145 {

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        node1.right = node2;
        node2.left = node3;
        System.out.println(preorderTraversal(node1));
    }

    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> container = new ArrayList<>();
        fullElement(container, root);
        return container;
    }

    private static void fullElement(List<Integer> container, TreeNode node) {
        if (node == null) {
            return;
        }
        TreeNode left = node.left;
        TreeNode right = node.right;
        if (left != null) {
            fullElement(container, left);
        }
        if (right != null) {
            fullElement(container, right);
        }
        container.add(node.val);
    }


}
